package com.gph.etrip;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.gph.etrip.timmy.test.Marker;
import com.gph.etrip.timmy.test.ViewAdapter;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ActivityLayout extends Activity {
//		public static FFDatabase ffDb;
		boolean hack = false;
		ArrayList<Marker> markerList = new ArrayList<Marker>();
		private TextView myTextView;
		private TextView myTextView1;
		private Button sendButton;
		private EditText editText;
		private RadioGroup radioGroup;
		private RadioButton radioButton1;
		private RadioButton radioButton2;
		private RadioButton radioButton3;
		private RadioButton radioButton4;
		private ImageView survImage;
		String name;
		String url;
		int markerId;
		ListView list;
		ViewAdapter adapter;

		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);

			// Init database
//			ffDb = new FFDatabase(this);
//			ffDb.open();

			setContentView(R.layout.activitylayout);
			Intent intent = getIntent();
			name = intent.getStringExtra("testIntent");
			url = intent.getStringExtra("testIntent1");
			// markerId= intent.getLongExtra("markerId");
			markerId = intent.getIntExtra("markerId", -1);

			Marker marker = new Marker(0, name, null, url);
			list = (ListView) findViewById(R.id.listView1);
			markerList.add(marker);
			adapter = new ViewAdapter(this, markerList);
			list.setAdapter(adapter);
			myTextView = (TextView) findViewById(R.id.myTextView);
			// myTextView.setText(name);
			myTextView1 = (TextView) findViewById(R.id.textView1);

			editText = (EditText) findViewById(R.id.distance);
			editText.setHint("Leave a note here...");
			// myButton = (Button)findViewById(R.id.gps_button);
			sendButton = (Button) findViewById(R.id.send_survey);
			sendButton.setOnClickListener(new MyButtonListener());
			radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);

			radioButton1 = (RadioButton) findViewById(R.id.radio1);
			radioButton2 = (RadioButton) findViewById(R.id.radio2);
			radioButton3 = (RadioButton) findViewById(R.id.radio3);
			radioButton4 = (RadioButton) findViewById(R.id.radio4);

			// Image
//			survImage = (ImageView) findViewById(R.id.imageSurv);
//			if (name.equals("Take Photo")) {
//				survImage.setVisibility(View.VISIBLE);
//			}

			OnClickListener radioClickListener = new OnClickListener() {
				public void onClick(View v) {
					if (v.getId() == radioGroup.getCheckedRadioButtonId() && hack) {

						radioGroup.clearCheck();
						myTextView1.setText("Current location");

					} else {

						hack = true;
					}
				}
			};
			OnCheckedChangeListener radioCheckChangeListener = new OnCheckedChangeListener() {
		
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {

					// editText.setHint("Current Location");
					if (radioButton1.isChecked() == true)
						myTextView1.setText(radioButton1.getText());
					if (radioButton2.isChecked() == true)
						myTextView1.setText(radioButton2.getText());
					if (radioButton3.isChecked() == true)
						myTextView1.setText(radioButton3.getText());
					if (radioButton4.isChecked() == true)
						myTextView1.setText(radioButton4.getText());
					// if(radioButton1.isChecked() == false &&
					// radioButton2.isChecked() == false
					// && radioButton3.isChecked() == false &&
					// radioButton4.isChecked() == false)

					hack = false;
					// editText.clearComposingText();
				}
			};

			radioButton1.setOnCheckedChangeListener(radioCheckChangeListener);
			radioButton2.setOnCheckedChangeListener(radioCheckChangeListener);
			radioButton3.setOnCheckedChangeListener(radioCheckChangeListener);
			radioButton4.setOnCheckedChangeListener(radioCheckChangeListener);

			radioButton1.setOnClickListener(radioClickListener);
			radioButton2.setOnClickListener(radioClickListener);
			radioButton3.setOnClickListener(radioClickListener);
			radioButton4.setOnClickListener(radioClickListener);

		}
		/**
		 * 
		 * Listener for sending the survey
		 *
		 */
		class MyButtonListener implements OnClickListener {


			public void onClick(View v) {

				// Toast.makeText(getApplicationContext(), "Marker: " + name,
				// Toast.LENGTH_LONG).show();
				
//				sendSurveyToMM();

				/*
				 * long longitude, latitude = getCurrentLocation(); Survey survey =
				 * new Survey();
				 * 
				 * survey.setLocation(longitude, latitude);
				 * survey.setMarker(marker); survey.setUser(user);
				 * 
				 * Database.saveSurvey(survey);
				 * 
				 * intent.setClass(SingleMarkerView.this,GpsLocation.class);
				 * SingleMarkerView.this.startActivity(intent);
				 */
			}
			/**
			 * Method for transmitting the survey to the management back-end
			 */
//			public void sendSurveyToMM() {
//				// Create a new HttpClient and Post Header
//				HttpClient httpclient = new DefaultHttpClient();
//				HttpPost httppost = new HttpPost(
//						"http://megs.myselfzjp.se/index.php/c_survey_handler/receive_ff");
//
//				try {
//					// Current time; can get from DB instead so its synced with GPS
//					// data
//					long currentTimestamp = System.currentTimeMillis() / 1000;
//					// Get GPS data from DB as added by service
//					Cursor cGps = ffDb.getGpsData();
//					cGps.moveToFirst();
//					String lat = cGps.getString(0);
//					String lon = cGps.getString(1);
//					// Get marker id based on the name from list
//					int mId = 1;
//
//					if (name.equals("Play4free")) {
//						mId = 4;
//						// Log.d("Marker id: ", mId + "");
//					} else if (name.equals("Lets Deal")) {
//						mId = 7;
//					} else if (name.equals("HM")) {
//						mId = 9;
//					} else if (name.equals("Zara")) {
//						mId = 10;
//					} else if (name.equals("Lisebery")) {
//						mId = 3;
//					} else if (name.equals("Hosteing")) {
//						mId = 2;
//					} else if (name.equals("Burgerking")) {
//						mId = 1;
//					}
//					String surveyXml = "<?xml version='1.0' encoding='UTF-8'?><survey userId=\"5\" incidentId=\"7\"><marker markerTime=\""
//							+ currentTimestamp
//							+ "\" markerId=\""
//							+ mId
//							+ "\" latitude=\""
//							+ lon
//							+ "\" longitude=\""
//							+ lat
//							+ "\" photo=\"photo/wood.jpg\" text=\""
//							+ editText.getText().toString() + "\"/></survey>";
//					// Add your data
//					List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//					nameValuePairs.add(new BasicNameValuePair("data", surveyXml));
//
//					httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//
//					// Execute HTTP Post Request
//					HttpResponse response = httpclient.execute(httppost);
//					
//					Toast.makeText(getApplicationContext(), "Marker data sent.", Toast.LENGTH_LONG).show();
//					Intent intent= new Intent();
//					intent.setClass(SingleMarkerView.this,RewardsListView.class);
//					SingleMarkerView.this.startActivity(intent);
//
//				} catch (ClientProtocolException e) {
//					Log.d("Send Failed:", "Client protocol exception");
//				} catch (IOException e) {
//					Log.d("Send Failed:", "IO exception");
//				} catch (Exception e) {
//					Log.d("Send Failed:", "Fail");
//				}
//			}

		}
	}
